package com.ruoyi.mdm.utils;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * 签名和验证签名工具类
 */
public class ApiSignatureUtils {

    /**
     * 创建md5摘要,
     *
     * @param body
     * @param appId     appid
     * @param nonce     流水号
     * @param timestamp 时间戳
     * @param appsecret 秘钥
     * @return 签名串
     */
    public static String createSign(String body, String appId, String nonce, String timestamp, String appsecret) {
        StringBuffer sb = new StringBuffer();
        sb.append("appid=" + appId);
        sb.append("nonce=" + nonce);
        sb.append("timestamp=" + timestamp);
        sb.append(body);
        sb.append("appsecret=" + appsecret);
        System.out.println("HMAC source:{" + sb.toString() + "}");
        // 签名
        String sign = DigestUtils.md5Hex(sb.toString()).toUpperCase();
        System.out.println("HMAC:{" + sign + "}");

        return sign;
    }

    public static void main(String[] args) {

        String body="{\t\"metricCode\": \"DIP_WECOM-1_COUPON_ORDER_DAILY\",\t\"systemCode\": \"WECOM-1\",\t\"metricDataTs\": 1719468326,\t\"metricValue\": 100}";
//        String body="{\t\"metricCode\": \"DIP_WECOM-1_COUPON_ORDER_DAILY\",\t\"systemCode\": \"DIP\",\t\"metricDataTs\": 1719467967,\t\"metricValue\": 100}";
        createSign(body,
                "shiseido3aa7cd99f6ed",
                "170442310888",
                "1719468326",
                "a1d433d2af7e81a17892def2e0eace88"
        );
    }

}
